/**
 * <pre>
 * Title: 		ButtonContainerTag.java
 * Author:		linriqing
 * Create:	 	2011-7-10 下午05:50:53
 * Copyright: 	Copyright (c) 2011
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.common.web.tags;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.util.Assert;

import com.hengpeng.common.acl.vo.ButtonObject;

/**
 * <pre>
 *  输入类型说明
 * </pre>
 * @author linriqing
 * @version 1.0, 2011-7-10
 */
public class ButtonContainerTag extends BodyTagSupport
{
	private static final long serialVersionUID = 7453479603706432623L;

	public static final String EXPRESSION_PREFIX = "@{";

	public static final String EXPRESSION_SUFFIX = "}";

	private List<ButtonObject> buttons;

	public ButtonContainerTag()
	{
		super();
		this.buttons = new ArrayList<ButtonObject>();
	}

	@Override
	public int doStartTag() throws JspException
	{
		this.buttons.clear();
		return super.doStartTag();
	}


	/**
	 * @return buttons
	 */
	public List<ButtonObject> getButtons()
	{
		return buttons;
	}

	/**
	 * @param buttons buttons
	 */
	public void setButtons(List<ButtonObject> buttons)
	{
		this.buttons = buttons;
	}

	public String getUrlByLink(String link)
	{
		int indexOf = link.indexOf('?');
		if (indexOf > -1)
		{
			return link.substring(0, indexOf);
		}
		return link;
	}

	public String getUrlParamByLink(String link)
	{
		StringBuffer sb = new StringBuffer();
		int indexOf = link.indexOf('?');
		sb.append("{");
		if (indexOf > -1)
		{
			String param = link.substring(indexOf + 1, link.length());
			String[] params = param.split("&");
			for (String string : params)
			{
				String[] keyValue = string.split("=");
				sb.append("'");
				sb.append(keyValue[0]);
				sb.append("':'");
				sb.append(keyValue[1]);
				sb.append("',");
			}
			if (sb.length() > 0)
			{
				sb.deleteCharAt(sb.length() - 1);
			}
		}
		sb.append("}");
		return sb.toString();
	}

	public Object evaluate(String source, Class<?> resultClass, PageContext pageContext) throws JspException
	{
		Assert.notNull(source, "Attribute value must not be null");
		Assert.notNull(resultClass, "Result class must not be null");
		Assert.notNull(pageContext, "PageContext must not be null");
		try
		{
			if (resultClass.isAssignableFrom(String.class))
			{
				StringBuilder resultValue = null;
				int exprPrefixIndex;
				int exprSuffixIndex = 0;
				do
				{
					exprPrefixIndex = source.indexOf(EXPRESSION_PREFIX, exprSuffixIndex);
					if (exprPrefixIndex != -1)
					{
						int prevExprSuffixIndex = exprSuffixIndex;
						exprSuffixIndex = source.indexOf(EXPRESSION_SUFFIX,
								exprPrefixIndex + EXPRESSION_PREFIX.length());
						String expr;
						if (exprSuffixIndex != -1)
						{
							exprSuffixIndex += EXPRESSION_SUFFIX.length();
							expr = source.substring(exprPrefixIndex, exprSuffixIndex);
						}
						else
						{
							expr = source.substring(exprPrefixIndex);
						}
						if (expr.length() == source.length())
						{
							// A single expression without static prefix or suffix ->
							// parse it with the specified result class rather than String.
							return evaluateExpression(source, Object.class, pageContext);
						}
						else
						{
							// We actually need to concatenate partial expressions into a String.
							if (resultValue == null)
							{
								resultValue = new StringBuilder();
							}
							resultValue.append(source.substring(prevExprSuffixIndex, exprPrefixIndex));
							resultValue.append(evaluateExpression(expr, String.class, pageContext));
						}
					}
					else
					{
						if (resultValue == null)
						{
							resultValue = new StringBuilder();
						}
						resultValue.append(source.substring(exprSuffixIndex));
					}
				}
				while (exprPrefixIndex != -1 && exprSuffixIndex != -1);
				return resultValue.toString();
			}
			else
			{
				return evaluateExpression(source, resultClass, pageContext);
			}
		}
		catch (ELException ex)
		{
			throw new JspException("Parsing of JSP EL expression failed for attribute '" + source + "'", ex);
		}
	}

	private Object evaluateExpression(String expr, Class<?> resultClass, PageContext pageContext) throws ELException
	{
		return pageContext.getExpressionEvaluator()
				.evaluate(expr, resultClass, pageContext.getVariableResolver(), null);
	}

}
